/*
 * aksImage.c
 *
 *  Created on: 2011/06/18
 *      Author: AKS
 */

#include "app/cv/include/aksImage.h"
#include "core/include/aks_processor.h"

AksPoint aksPoint(WORD w, WORD h)
{
	AksPoint pt;

	pt.x = w;
	pt.y= h;

	return pt;
}

AksPixel aksPixel(BYTE b, BYTE g, BYTE r)
{
	AksPixel px;

	px.R = r;
	px.G = g;
	px.B = b;

	return px;
}

AksImage *createImage(WORD width, WORD height, DWORD imgPtr, AksImage *dst, AksColorType type, AksPoint offset)
{
    dst->imgPtr      = imgPtr;
    dst->width       = width;
    dst->widthStep   = width * 3;
    dst->height      = height;
    dst->colorType   = type;
    dst->depth       = 8;
    dst->offset      = offset;

	return dst;
}

extern inline void aksSet2D(AksImage *img, DWORD w, DWORD h, AksPixel pixel)
{
//	img->imgPtr[(h + img->offset.height)*img->widthStep + (w + img->offset.width) + 0] = pixel.R;
//	img->imgPtr[(h + img->offset.height)*img->widthStep + (w + img->offset.width) + 1] = pixel.G;
//	img->imgPtr[(h + img->offset.height)*img->widthStep + (w + img->offset.width) + 2] = pixel.B;

//	DWORD idx = img->imgPtr + 3*(((h + img->offset.y)*img->width) + (w + img->offset.x));
	DWORD idx = img->imgPtr + 3*(((h)*img->width) + (w));


	EXTBUS_write(idx + 0, pixel.R);
	EXTBUS_write(idx + 1, pixel.G);
	EXTBUS_write(idx + 2, pixel.B);

	return;
}

extern inline AksPixel aksGet2D(AksImage *img, DWORD w, DWORD h)
{
//	DWORD idx = img->imgPtr + 3*(((h + img->offset.y)*img->width) + (w + img->offset.x));
	DWORD idx = img->imgPtr + 3*(((h)*img->width) + (w));


	return aksPixel
	    (
	        EXTBUS_read(idx + 2), //   img->imgPtr[(h + img->offset.height)*img->widthStep + (w + img->offset.width) + 2],
	    	EXTBUS_read(idx + 1), //   img->imgPtr[(h + img->offset.height)*img->widthStep + (w + img->offset.width) + 1],
	    	EXTBUS_read(idx + 0)  //   img->imgPtr[(h + img->offset.height)*img->widthStep + (w + img->offset.width) + 0]
		);
}


void aksSetZero(AksImage *img)
{
	AksPixel blank = aksPixel(0,0,0);

	for(WORD i = 0 ; i < img->height ; i++)
	{
	    for(WORD j=0 ; j< img->width ; j++)
		{
            aksSet2D(img, j, i, blank);
		}
	}

	return;
}

